﻿@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title></title>
</head>
<body>
    <div class="layui-container">
        <div class="page-header">
            <span class="layui-breadcrumb" style="visibility:visible;">
                <a href="/System/Organization/Organization">组织结构</a>
                <a><cite>角色授权( <span class="layui-badge layui-bg-blue">@ViewBag.deptName</span>  <i class="layui-icon layui-icon-triangle-r"></i> <span class="layui-badge layui-bg-green">@ViewBag.roleName</span>)</cite></a>
            </span>
        </div>
        <div class="layui-row">
            <!--<div class="layui-col-md3" style="text-align:center;margin-top:50px">
               -->
                <!--输入框-->
                <!--<div class="layui-row">
                    <label>角色名称：</label>
                    <label id="roleName">@ViewBag.roleName</label>
                </div>
                <div class="layui-row">
                    <label>所属部门：</label>
                    <label id="cmdName">@ViewBag.deptName</label>
                </div>
            </div>-->
            <div class="layui-col-md12">
                <div id="treebox"></div>
                <div class="layui-col-md6 layui-col-md-offset1" style="margin-top:20px">
                    <input type="hidden" id="roleID" value="@ViewBag.roleID" />
                    <input type="button" class="layui-btn" value="保存设置" />
                </div>
                
                @*<input type="button" class="layui-btn" value="设置选中值" />*@
            </div>

        </div>
    </div>

    <script type="text/javascript">
        layui.use(["element", "tree","layer"], function () {
            var element = layui.element;
            var tree = layui.tree;
            var layer = layui.layer;

            var index = layer.load(2);
            $.post("/System/Menus/GetMenusAuthority", { rid: $("#roleID").val() }, function (data) {
                var d = JSON.parse(data);
                //console.log(d);
                //将数据转换成 tree 需要的格式
                var treedata = [];
                $(d).each(function (i) {
                    //一级菜单
                    if (this.pid == 0) {
                        //定义2级子菜单
                        var $level2 = [];
                        //定义1级菜单数据
                        var $level1 = {
                            title: this.title,
                            id: this.id,
                            menuID: this.id,
                            roleID: this.rid,
                            icon: this.icon,
                            //checked: false,
                            //spread: true,
                            children: $level2
                        }
                        //添加一级菜单
                        treedata.push($level1);
                        var mid = this.id;
                        //循环加入子菜单
                        $(d).each(function (i) {
                            if (this.pid == mid) {
                                //添加二级菜单
                                $level2.push({
                                    title: this.title,
                                    id: this.id,
                                    icon: this.icon,
                                    menuID: this.id,
                                    roleID: this.rid,
                                    checked: this.check
                                });
                            }
                        })
                    }

                });
                tree.render({
                    id: "mytree",
                    elem: "#treebox",
                    showCheckbox: true,
                    showLine: false,
                    data: treedata,
                    click: function (obj) {
                        console.log(obj);
                    }
                });
                layer.close(index);
            })

            $("input[type=button]:eq(0)").on("click", function () {
                var checkdata = tree.getChecked("mytree");
                var data = {
                    "mids": [],
                    "rid": $("#roleID").val()
                };
                $(checkdata).each(function (i) {
                    data.mids.push(this.id);
                    $(this.children).each(function () {
                        data.mids.push(this.id);
                    })
                });
                $.post("/System/Menus/SetMenusAuthority", data, function (data) {
                    var d = JSON.parse(data);
                    if (d.code > 0) {
                        parent.layer.msg(d.msg, { icon: 6 });
                    } else {
                        parent.layer.msg(d.msg, { icon: 5 });
                    }
                })
            });
            //$("input[type=button]:eq(1)").on("click", function () {
            //    tree.setChecked("mytree", [122, 113])
            //});
        })
    </script>
</body>
</html>
